package cn.edu.xmu.ooad.sortwords;

import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SortWordFirst {

    private static int score(String word){
        //非a字母数
        return word.replaceAll("a", "").length();
    }

    public static Comparator<String> scoreComparator = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return Integer.compare(score(o2), score(o1));
        }
    };

    /**
     * 原始的Java实现
     * @param words 单词列表
     * @return 排序的单词列表
     */
    public static List<String> rank(List<String> words){
        words.sort(scoreComparator);
        return words;
    }

    /**
     * 采用Java Stream的实现
     * @param words 单词列表
     * @return 排序的单词列表
     */
    public static List<String> rankWithStream(List<String> words){
        return words.stream().sorted(scoreComparator).collect(Collectors.toUnmodifiableList());
    }

    /**
     * 采用Java Stream，带比较函数的实现
     * @param words 单词列表
     * @param comparator 比较函数
     * @return 排序的单词列表
     */
    public static List<String> rankWithComparator(List<String> words, Comparator<String> comparator){
        return words.stream().sorted(comparator).collect(Collectors.toUnmodifiableList());
    }
    /**
     * 采用Java Stream，带score函数的实现
     * @param words 单词列表
     * @param scoreFunction 计分函数
     * @return 排序的单词列表
     */
    public static List<String> rankWithScoreFunc(List<String> words, Function<String, Integer> scoreFunction){

        Comparator<String> comparator = (w1, w2)->
                Integer.compare(scoreFunction.apply(w2), scoreFunction.apply(w1));

        return words.stream().sorted(comparator).collect(Collectors.toUnmodifiableList());
    }

    /**
     * 返回高于score分数的单词
     * @param words 单词列表
     * @param scoreFunction 计分函数
     * @return 排序的单词列表
     */
    public static List<String> highScoreWords(List<String> words, Function<String, Integer> scoreFunction, int score){
        return words.stream().filter(w -> scoreFunction.apply(w) > score)
                .sorted( (w1, w2)-> Integer.compare(scoreFunction.apply(w2), scoreFunction.apply(w1)))
                .collect(Collectors.toUnmodifiableList());
    }

    /**
     * 返回高于score分数的单词
     * @param words 单词列表
     * @param scoreFunction 计分函数
     * @return 函数
     */
    public static FiltWord highScoreWords(List<String> words, Function<String, Integer> scoreFunction){
        return score ->
                words.stream()
                        .filter(w -> scoreFunction.apply(w) > score)
                        .sorted( (w1, w2)-> Integer.compare(scoreFunction.apply(w2), scoreFunction.apply(w1)))
                        .collect(Collectors.toUnmodifiableList());
    }

    public static Function<Function<String, Integer>, Function<Integer, List<String>>> highScoreWordsCurry(List<String> words) {
        return scoreFunction ->
                score ->
                        words.stream()
                                .filter(w -> scoreFunction.apply(w) > score)
                                .sorted( (w1, w2)-> Integer.compare(scoreFunction.apply(w2), scoreFunction.apply(w1)))
                                .collect(Collectors.toUnmodifiableList());
    }

}
